<?php
/**
  * //w/w/w.s/h/u/o/t//u/p/u c/o/m  说 图/谱/源/码/分/享
 */
defined("IN_IA")or exit("Access denied");
global $_GPC,$_W;
$uniacid=$_GPC['uniacid'];
$op=$_GPC['op'] ? $_GPC['op'] :"index";
$uid=$_GPC['uid'];

//积分商城首页数据
if($op=="getIntrgralData"){
	$request=array();
	$userData=pdo_get('cqkundian_farm_user',array('uid'=>$uid,'uniacid'=>$uniacid));
	$request['userData']=$userData;
	//获取商品分类信息
	$typeData=pdo_getall('cqkundian_farm_integral_goods_type',array('uniacid'=>$uniacid));
	$request['typeData']=$typeData;
	//广告图
	$slideData=pdo_getall('cqkundian_farm_slide',array('slide_type'=>3,'status'=>1,'uniacid'=>$uniacid));
	$request['slideData']=$slideData;
	//推荐热品
	$recommendData=pdo_getall('cqkundian_farm_integral_goods',array('is_put_away'=>1,'is_recommend'=>1,'uniacid'=>$uniacid));
	$request['recommendData']=$recommendData;
	echo json_encode($request);die;
}

//获取商品详细信息
if($op=='getIntegralGoodsDetail'){
	$request=array();
	$goodsid=$_GPC['goods_id'];
	$goodsData=pdo_get('cqkundian_farm_integral_goods',array('id'=>$goodsid,'uniacid'=>$uniacid));
	$goodsData['goods_slide']=unserialize($goodsData['goods_slide']);
	$request['goodsData']=$goodsData;
	//获取商品规格信息
	$specItem=pdo_getall('cqkundian_farm_integral_spec',array('goods_id'=>$goodsid,'uniacid'=>$uniacid));

	//获取规格值信息
	for ($i=0; $i < count($specItem); $i++) {
		$specVal=pdo_getall('cqkundian_farm_integral_spec_value',array('uniacid'=>$uniacid,'spec_id'=>$specItem[$i]['id']));
		$specItem[$i]['select_spec']=0;  //未选择
		for ($j=0; $j <count($specVal) ; $j++) {
			$specVal[$j]['select_val']=0;	
		}
		$specItem[$i]['specVal']=$specVal;
	}
	$request['specItem']=$specItem;
	echo json_encode($request);die;
}

//动态获取规格值
if($op=='getSpec'){
	$request=array();
	$spec_id=explode(',', $_GPC['spec_id']);
	$goods_id=$_GPC['goodsid'];
	sort($spec_id);
	$sku_name=implode(',',$spec_id);
	$specVal=pdo_get('cqkundian_farm_integral_goods_spec',array('sku_name'=>$sku_name,'goods_id'=>$goods_id));
	$request['specVal']=$specVal;
	echo json_encode($request);die;
}

//确认下单获取商品信息和规格
if($op=='getSureGoods'){
	$request=array();
	$goods_id=$_GPC['goods_id'];
	$spec_id=$_GPC['spec_id'];
	$goodsData=pdo_getall('cqkundian_farm_integral_goods',array('id'=>$goods_id,'uniacid'=>$uniacid));
	$specItem=pdo_getall('cqkundian_farm_integral_spec',array('goods_id'=>$goods_id,'uniacid'=>$uniacid));
    //查询快递运费
    $aboutData=pdo_get('cqkundian_farm_about',array('uniacid'=>$uniacid));
	if($specItem){
		$specVal=pdo_get('cqkundian_farm_integral_goods_spec',array('goods_id'=>$goods_id,'id'=>$spec_id,'uniacid'=>$uniacid));
		$spec_id_arr=explode(',',$specVal['sku_name']);
		for ($i=0; $i <count($spec_id_arr) ; $i++) { 
			$spec_val=pdo_get('cqkundian_farm_integral_spec_value',array('id'=>$spec_id_arr[$i],'uniacid'=>$uniacid));
			for ($j=0; $j <count($specItem) ; $j++) { 
				if($specItem[$j]['id']==$spec_val['spec_id']){
					$specItem[$i]['spec_val']=$spec_val['spec_value'];
				}
			}
		}

		//订单总价
		$totalPrice=$specVal['price']*$_GPC['count'];
		if(empty($specVal['spec_src'])){
			$specVal['spec_src']=$goodsData[0]['cover'];
		}
        $request['specVal']=$specVal;
	}else{
		$totalPrice=$goodsData[0]['price']*$_GPC['count'];
	}
	$request['specItem']=$specItem;
	$request['goodsData']=$goodsData;
	$request['aboutData']=$aboutData;
    $send_price=0;
    for($j=0;$j<count($goodsData);$j++){
        $send_price+=$goodsData[$j]['send_price'];
    }
	$request['totalPrice']=$totalPrice;
	$request['send_price']=$send_price;
	echo json_encode($request);die;
}

//获取商品信息
if($op=='getGoodsList'){
	$request=array();
	if(!empty($_GPC['page'])){
		$page=intval($_GPC['page'])+1;
	}else{
		$page=1;
	}
	if(!empty($_GPC['rank'])){
		$rank=$_GPC['rank'];
		$rank_type=$_GPC['rank_type'];
		if($rank==1){
			$order='rank '.$rank_type;
		}elseif($rank==2){
			$order='sale_count '.$rank_type;
		}else{
			$order='price '.$rank_type;
		}
	}else{
		$order='rank asc';
	}
	$type_id=$_GPC['type_id'];
	$goods_where=array('type_id'=>$type_id,'uniacid'=>$uniacid,'is_put_away'=>1);
	$goodsData=pdo_getall('cqkundian_farm_integral_goods',$goods_where,'','',$order,array($page,16));
	$request['goodsData']=$goodsData;
	echo json_encode($request);die;
}

//生成订单
if($op=='addOrder'){
	$name=$_GPC['name'];
	$address=$_GPC['address'];
	$phone=$_GPC['phone'];
	$remark=$_GPC['remark'];
	$goods_id=$_GPC['goods_id'];
	$spec_id=$_GPC['spec_id'];
	$count=$_GPC['count'];
	$aboutData=pdo_get('cqkundian_farm_about',array('uniacid'=>$uniacid));
    $goodsData=pdo_get('cqkundian_farm_integral_goods',array('id'=>$goods_id,'uniacid'=>$uniacid));
	$insertData=array(
		'order_number'=>time().rand(100,999),
		'uid'=>$uid,
		'status'=>0,
		'create_time'=>time(),
		'name'=>$name,
		'address'=>$address,
		'phone'=>$phone,
		'uniacid'=>$uniacid,
		'remark'=>$remark,
		'send_price'=>$goodsData['send_price'],
	);

    $insertData['body']='积分兑换商品';
	$userData=pdo_get('cqkundian_farm_user',array('uid'=>$uid,'uniacid'=>$uniacid));
	if($goodsData['is_open_sku']==1){
		$specVal=pdo_get('cqkundian_farm_integral_goods_spec',array('goods_id'=>$goods_id,'id'=>$spec_id,'uniacid'=>$uniacid));
		$insertData['total_price']=$specVal['price']*$count;
		//判断该用户积分是否足够兑换该商品
		if($userData['score']<$insertData['total_price']){
			echo json_encode(array('code'=>3));die;  //积分不足
		}
		$order_res=pdo_insert('cqkundian_farm_integral_order',$insertData);
		$order_id=pdo_insertid();
		//商品详情
		$insertDetail=array(
			'goods_id'=>$goods_id,
			'order_id'=>$order_id,
			'goods_name'=>$goodsData['goods_name'],
			'price'=>$specVal['price'],
			'count'=>$count,
			'spec_id'=>$spec_id,
			'uniacid'=>$uniacid,
		);
		if($specVal['spec_src']){
			$insertDetail['cover']=$specVal['spec_src'];
		}else{
			$insertDetail['cover']=$goodsData['cover'];
		}
	}else{		//没有规格
        $insertData['total_price']=$goodsData['price']*$count;
        //判断该用户积分是否足够兑换该商品
		if($userData['score']<$insertData['total_price']){
			echo json_encode(array('code'=>3));die;  //积分不足
		}
        $order_res=pdo_insert('cqkundian_farm_integral_order',$insertData);
        $order_id=pdo_insertid();
		//商品详情
		$insertDetail=array(
			'goods_id'=>$goods_id,
			'order_id'=>$order_id,
			'goods_name'=>$goodsData['goods_name'],
			'price'=>$goodsData['price'],
			'count'=>$count,
			'spec_id'=>$spec_id,
			'uniacid'=>$uniacid,
			'cover'=>$goodsData['cover'],
		);
	}
	$detail_res=pdo_insert('cqkundian_farm_integral_order_detail',$insertDetail);
	if($order_res && $detail_res){
        echo json_encode(array('code'=>1,'order_id'=>$order_id));die;
	}else{
		echo json_encode(array('code'=>2));die;
	}
}

if($op=='notify'){
    $order_id=$_GPC['order_id'];
    $orderData=pdo_get('cqkundian_farm_integral_order',array('uniacid'=>$uniacid,'id'=>$order_id));
    if($orderData['status']==1) {
        //更新用户积分
        $user_res = pdo_update('cqkundian_farm_user', array('score -=' => $orderData['total_price']), array('uid' => $uid, 'uniacid' => $uniacid));
        if ($user_res) {
            //向商家发送模板消息
            include 'function.inc.php';
            $wxData = pdo_get('cqkundian_farm_wx_set', array('uniacid' => $uniacid));
            $wx_openid = unserialize($wxData['get_openid']);
            for ($i = 0; $i < count($wx_openid); $i++) {
                $res_send_shop = send_template_message($wx_openid[$i], $orderData, $uniacid);
            }
            //更新商品销量库存
            updateSaleCount($order_id, $uniacid);
            updateGoodsCount($order_id, $uniacid);
            insertIntegral($uniacid, $order_id);
            echo json_encode(array('code' => 1, 'order_id' => $order_id));
            die;
        } else {
            echo json_encode(array('code' => 4, 'order_id' => $order_id));
            die;        //订单支付失败
        }
    }else{
        echo json_encode(array('code' => 4, 'order_id' => $order_id));
        die;
    }
}

//获取兑换记录
if($op=='getIntegralRecord'){
	$request=array();
	if(!empty($_GPC['page'])){
		$page=intval($_GPC['page'])+1;
	}else{
		$page=1;
	}
	$orderData=pdo_getall('cqkundian_farm_integral_order',array('uid'=>$uid,'uniacid'=>$uniacid),'','','create_time desc',array($page,10));
	for($i=0;$i<count($orderData);$i++){
		$orderDetail=pdo_get('cqkundian_farm_integral_order_detail',array('order_id'=>$orderData[$i]['id'],'uniacid'=>$uniacid));
		$orderData[$i]['goods_name']=$orderDetail['goods_name'];
		$orderData[$i]['cover']=$orderDetail['cover'];
		$orderData[$i]['create_time']=date("Y-m-d H:i:s",$orderData[$i]['create_time']);
	}
	$request['orderData']=$orderData;
	echo json_encode($request);die;
}

/**
 * 添加积分记录
 * @param $uniacid
 * @param $uid
 * @param $score
 */
function insertIntegral($uniacid,$order_id){
    $orderData=pdo_get('cqkundian_farm_integral_order',array('id'=>$order_id,'uniacid'=>$uniacid));
    $userData=pdo_get('cqkundian_farm_user',array('uid'=>$orderData['uid'],'uniacid'=>$uniacid));
    $insertData=array(
        'uid'=>$orderData['uid'],
        'uniacid'=>$uniacid,
        'create_time'=>time(),
        'score_type'=>2,
        'score'=>$orderData['total_price'],
        'body'=>$orderData['body'],
        'now_score'=>$userData['score'],
    );
    pdo_insert('cqkundian_farm_integral_record',$insertData);
}

/**
 * 更新商品销量
 * @param  [type] $order_id [description]
 * @param  [type] $uniacid  [description]
 * @return [type]           [description]
 */
function updateSaleCount($order_id,$uniacid){
	$orderData=pdo_get('cqkundian_farm_integral_order',array('id'=>$order_id,'uniacid'=>$uniacid));
	$orderDetail=pdo_getall('cqkundian_farm_integral_order_detail',array('order_id'=>$order_id,'uniacid'=>$uniacid));
	for($i=0;$i<count($orderDetail);$i++){
		$goods_where=array(
			'id'=>$orderDetail[$i]['goods_id'],
			'uniacid'=>$uniacid,
		);
		pdo_update('cqkundian_farm_integral_goods',array('sale_count +='=>$orderDetail[$i]['count']),$goods_where);
	}
}

/**
 * 更新商品库存
 * @param $orderid
 * @param $uniacid
 */
function updateGoodsCount($orderid,$uniacid){
    $orderData=pdo_get('cqkundian_farm_integral_order',array('id'=>$orderid,'uniacid'=>$uniacid));
    $orderDetail=pdo_get('cqkundian_farm_integral_order_detail',array('order_id'=>$orderid,'uniacid'=>$uniacid));
    $specVal=pdo_get('cqkundian_farm_integral_goods_spec',array('id'=>$orderDetail['spec_id']));
    if($orderDetail['spec_id']!=0){
        pdo_update('cqkundian_farm_integral_goods_spec',array('count -='=>$orderDetail['count']),array('id'=>$orderDetail['spec_id'],'uniacid'=>$uniacid));
    }else{
        pdo_update('cqkundian_farm_integral_goods',array('count -='=>$orderDetail['count']),array('id'=>$orderDetail['goods_id'],'uniacid'=>$uniacid));
    }
}
